<template>
  <el-container class="home-container">
    <el-header height="60px" class="header">
      <div class="header-left">
        <el-icon><Menu /></el-icon>
        <span class="title">后台管理系统</span>
      </div>
      <div class="header-right">
        <span>{{ userStore.user?.name || '管理员' }}</span>
        <el-button type="text" @click="handleLogout">退出登录</el-button>
      </div>
    </el-header>
    <el-container>
      <el-aside width="200px" class="aside">
        <el-menu :default-active="activeMenu" class="el-menu-vertical" router :unique-opened="true">
          <el-sub-menu index="system">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>系统菜单</span>
            </template>
            <el-menu-item index="/users">
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </el-menu-item>
            <el-menu-item index="/files">
              <el-icon><Document /></el-icon>
              <span>资料管理</span>
            </el-menu-item>
             <el-menu-item index="/courses">
              <el-icon><School /></el-icon>
              <span>课程管理</span>
            </el-menu-item>
            <el-menu-item index="/notices">
              <el-icon><Message /></el-icon>
              <span>信息发布</span>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      <el-main class="main">
        <router-view :key="$route.fullPath" />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Menu, Setting, User, UserFilled, Document, Message, School } from '@element-plus/icons-vue'
import { useUserStore } from '../store/user'

export default {
  name: 'Home',
  components: {
    Menu,
    Setting,
    User,
    UserFilled,
    Document,
    Message,
    School
  },
  setup() {
    const route = useRoute()
    const router = useRouter()
    const userStore = useUserStore()
    
    const activeMenu = computed(() => {
      // 使用路由路径进行匹配
      const path = route.path;
      if (path.startsWith('/users')) return '/users';
      if (path.startsWith('/files')) return '/files';
      if (path.startsWith('/notices')) return '/notices';
      if (path.startsWith('/courses')) return '/courses';
      return '/users'
    })
    
    const handleLogout = () => {
      userStore.logout()
      router.push('/login')
      ElMessage.success('已退出登录')
    }
    
    onMounted(async () => {
      await userStore.fetchUserInfo()
      // 如果当前路由是根路径，重定向到users页面
      if (route.path === '/') {
        router.push('/users')
      }
    })
    
    return {
      userStore,
      activeMenu,
      handleLogout
    }
  }
}
</script>

<style scoped>
.home-container {
  height: 100vh;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.aside {
  background-color: #f0f2f5;
  height: calc(100vh - 60px);
}

.main {
  padding: 20px;
  height: calc(100vh - 60px);
  overflow-y: auto;
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from, .fade-leave-to {
  opacity: 0;
}
</style>